<?php

	$root_path = str_replace('\\', '/',  dirname(dirname(__FILE__))). '/';
	$inst_path = str_replace('\\', '/',  dirname(__FILE__)). '/';
	
	define('IN_SCHOOL',1);
	$step = isset($_GET['step']) ? abs(intval($_GET['step'])) : 1;

?><!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">
	<meta name="viewport" content="width=device-width,minimum-scale=1,maximum-scale=1">
	<title>均源v1.0.6 RC 安装程序</title>
	<link rel="shortcut icon" href="../favicon.ico" />
	<link href="../jy_src/css/jy_common.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div>
<div class="nav">均源安装程序 Step:<?php echo $step; ?></div>
<?php
file_exists($inst_path. 'install.lock') && $step = 0;


switch($step) {
case 1:
	?><div class="t">开始安装</div>
	<div class="l2">检测文件系统权限：</div><?php
	require_once $root_path.'jy_core/BASE_DRIVERS/CORE/misc.php';
	$errno = 0;
	$filearr = array( 'jy_core/LOGS/','jy_core/TMPS/','jy_conf/', 'jy_control/',
				'jy_model/','jy_pages/template/','jy_pages/cache/', 'jy_plugin/',
				'jy_uploads/', 'jy_uploads/headphoto/','jy_uploads/thumbnail/');
	foreach($filearr as $k=>$v) {
		?><div class="l2"><?php
		if(misc::is_writable($root_path.$v)) {
			echo '<div style="color:green;">[/'.$v.']可写</div>';
		}else{
			$errno = 1;
			echo '<div style="color:red;">[/'.$v.']不可写</div>';
		}
		?></div><?php
	}
	if($errno == 0){
		?><div class="f"><a href="../jy_install/index.php?step=2">检测通过，进入下一步</a></div><?php
	}else{
		?><div class="f">检测未通过，请先解决以上问题</div><?php
	}
	break;
case 2:
	$CONFIG_APP_HOST = isset($_POST['CONFIG_APP_HOST']) ?$_POST['CONFIG_APP_HOST']:'';
	$CONFIG_APPDB_PRE = isset($_POST['CONFIG_APPDB_PRE']) ?$_POST['CONFIG_APPDB_PRE']:'';
	$CONFIG_APPDB_MASTER_HOST = isset($_POST['CONFIG_APPDB_MASTER_HOST']) ?$_POST['CONFIG_APPDB_MASTER_HOST']:'';
	$CONFIG_APPDB_MASTER_NAME = isset($_POST['CONFIG_APPDB_MASTER_NAME']) ?$_POST['CONFIG_APPDB_MASTER_NAME']:'';
	$CONFIG_APPDB_MASTER_USER = isset($_POST['CONFIG_APPDB_MASTER_USER']) ?$_POST['CONFIG_APPDB_MASTER_USER']:'';
	$CONFIG_APPDB_MASTER_PASS = isset($_POST['CONFIG_APPDB_MASTER_PASS']) ?$_POST['CONFIG_APPDB_MASTER_PASS']:'';
	$CONFIG_COOKIE_PRE = isset($_POST['CONFIG_COOKIE_PRE']) ?$_POST['CONFIG_COOKIE_PRE']:'';
	$CONFIG_COOKIE_TIME = isset($_POST['CONFIG_COOKIE_TIME']) ?$_POST['CONFIG_COOKIE_TIME']:'';
	$CONFIG_ONLINE_TIME = isset($_POST['CONFIG_ONLINE_TIME']) ?$_POST['CONFIG_ONLINE_TIME']:'';

	$done = 0;
	empty($CONFIG_APP_HOST) && $done--;
	empty($CONFIG_APPDB_PRE) && $done--;
	empty($CONFIG_APPDB_MASTER_HOST) && $done--;
	empty($CONFIG_APPDB_MASTER_NAME) && $done--;
	empty($CONFIG_APPDB_MASTER_USER) && $done--;
	empty($CONFIG_APPDB_MASTER_PASS) && $done--;
	empty($CONFIG_COOKIE_PRE) && $done--;
	empty($CONFIG_COOKIE_TIME) && $done--;
	empty($CONFIG_ONLINE_TIME) && $done--;
	
	?><div class="t">配置设置</div><?php  
	if($done == 0) {
		$s = file_get_contents($root_path.'jy_conf/config_temp.php');
		$s = str_replace('{{{CONFIG_APP_HOST}}}',$CONFIG_APP_HOST,$s);
		$s = str_replace('{{{CONFIG_APPDB_PRE}}}',$CONFIG_APPDB_PRE,$s);
		$s = str_replace('{{{CONFIG_APPDB_MASTER_HOST}}}',$CONFIG_APPDB_MASTER_HOST,$s);
		$s = str_replace('{{{CONFIG_APPDB_MASTER_NAME}}}',$CONFIG_APPDB_MASTER_NAME,$s);
		$s = str_replace('{{{CONFIG_APPDB_MASTER_USER}}}',$CONFIG_APPDB_MASTER_USER,$s);
		$s = str_replace('{{{CONFIG_APPDB_MASTER_PASS}}}',$CONFIG_APPDB_MASTER_PASS,$s);
		$s = str_replace('{{{CONFIG_COOKIE_PRE}}}',$CONFIG_COOKIE_PRE,$s);
		$s = str_replace('{{{CONFIG_COOKIE_TIME}}}',$CONFIG_COOKIE_TIME,$s);
		$s = str_replace('{{{CONFIG_ONLINE_TIME}}}',$CONFIG_ONLINE_TIME,$s);
	
		$s = str_replace('{{{CONFIG_CS_KEY}}}',md5('CONFIG_CS_KEY'.$CONFIG_APP_HOST.rand(1000,9999).time()),$s);
		$s = str_replace('{{{USER_KEY}}}',md5('USER_KEY'.$CONFIG_APP_HOST.rand(1000,9999).time()),$s);
		file_put_contents($root_path.'jy_conf/config.php',$s,LOCK_EX);
	
		?><div class=""></div>
		<div class="f"><a href="../jy_install/index.php?step=3">保存成功，进入下一步</a></div><?php
	}else{
	?><div class="f">
		<form action="../jy_install/index.php?step=2" method="post">
			<p>域名或IP(不带http://)<br/><?php
			$php_self_path = $_SERVER['PHP_SELF'] == '' ? $_SERVER['REQUEST_URI'] : $_SERVER['PHP_SELF'];
			$arr = explode('/', $_SERVER['SERVER_NAME'].$php_self_path);
			array_pop($arr);//1
			array_pop($arr);//2
			$host_path = implode('/',$arr);
			?>
			<input type="text" name="CONFIG_APP_HOST" value="<?php echo $host_path; ?>"/></p>
			<?php
				unset($php_self_path);
				unset($arr);
				unset($host_path);
			?>
			<p>数据表前缀<br/>
			<input type="text" name="CONFIG_APPDB_PRE" value="jy_"/></p>
			<p>数据库地址<br/>
			<input type="text" name="CONFIG_APPDB_MASTER_HOST" value="127.0.0.1"/></p>
			<p>数据库名<br/>
			<input type="text" name="CONFIG_APPDB_MASTER_NAME" value=""/></p>
			<p>数据库用户名<br/>
			<input type="text" name="CONFIG_APPDB_MASTER_USER" value=""/></p>
			<p>数据库密码<br/>
			<input type="text" name="CONFIG_APPDB_MASTER_PASS" value=""/></p>
			<p>COOKIE前缀<br/>
			<input type="text" name="CONFIG_COOKIE_PRE" value="jy_"/></p>
			<p>COOKIE保存时间(秒)<br/>
			<input type="text" name="CONFIG_COOKIE_TIME" value="31536000"/></p>
			<p>用户在线时长(秒)<br/>
			<input type="text" name="CONFIG_ONLINE_TIME" value="1800"/></p>
			<p>
			<input type="submit" name="sub" value="保存配置"/></p>
		</form>
	</div><?php
	}
	break;
case 3:
	define('IN_APP',1);
	include $root_path. 'jy_conf/config.php';
	include $root_path. 'jy_core/BASE_DRIVERS/DB/db_interface.php';
	include $root_path. 'jy_core/BASE_DRIVERS/DB/db_pdo_mysql.php';
	include $root_path. 'jy_core/BASE_DRIVERS/base_driver.class.php';
	
	function make_ssid($name ,$pass) {
		return md5(md5($pass.'@'.USER_KEY).$name);
	}
	$s = file_get_contents($inst_path. 'junyuandb.sql');
	$s = str_replace('{{{CONFIG_APPDB_PRE}}}',CONFIG_APPDB_PRE,$s);
	$s = str_replace('{{{ADMINSSID}}}',make_ssid('admin' , 'admin'),$s);
	$s = str_replace('{{{SYSTEMSSID}}}',make_ssid('系统' ,md5(rand(10000,99999))),$s);
	$arr = explode(";\n",$s);
	$i = 0;
	$err = array();
	foreach($arr as $k=>$sql) {
		if(strlen($sql) >0 ){
			if(FALSE!==$DB->query($sql)){
				$i++;
			}else{
				$err[] = $sql;
			}
		}
	}
	if(empty($err)){
		$config_file_path = $root_path. 'jy_conf/config.php';
		$s = file_get_contents($config_file_path);
		$s = str_replace("define('CONFIG_INSTALLED', 0);","define('CONFIG_INSTALLED', 1);",$s);
		file_put_contents($config_file_path,$s,LOCK_EX);
		file_put_contents($inst_path. 'install.lock','installed',LOCK_EX);
		?><div class="l2">数据库导入成功！均源完成安装，建议删除/jy_install/，<a href="../">进入首页</a></div><?php
	}else{
		?><div class="t">安装出错，错误SQL:</div><?php
		foreach($err as $k=>$v) {
			?><div class="l2"><?php echo $v;?></div><?php
		}
	}
	break;
default:
	?><div class="l2">安装程序已锁定</div><?php
	break;
}

?></div></body>
</html>